let joblist = (function () {
    let $tableBox = $('.tableBox'),
        $tbody = $tableBox.find('tbody')
    
    // 获取职务列表
    async function bindJobList() {
        let result = await queryJob()
        if (result.code != 0) return
        let str = ``
        let obj={userhandle:'员工操作权',departhandle:'部门操作权',jobhandle:'职务操作权',customermy:'部门全部客户',customerall:'公司全部客户',resetpassword:'重置密码 '}
        result.data.forEach((item,index) => { 
            let { id, name, desc, power } = item 
            let arr = []
            item.name
            power.split('|').forEach(item => {
                arr.push(obj[item])
            })
            power = arr.join('|')
            arr=null
            str+=`<tr>
                <td class="w8">${index+1}</td>
                <td class="w10">${name}</td>
                <td class="w20">${desc}</td>
                <td class="w50">${power}</td>
                <td class="w12" jobid=${id}>
                    <a href="javascript:;" class="btnEdit">编辑</a>
                    <a href="javascript:;" class="btnDelete">删除</a>
                </td>
            </tr>`
        })
        $tbody.html(str)

    }

    function delegateHandle() {
        $tbody.click(async ev => {
            let target = ev.target
            let $target = $(target)
            let jobid= $target.parent().attr('jobid')
            if (target.className === 'btnEdit') {
                window.location.href='jobadd.html?jobId='+jobid
                return
            }
            if (target.className === 'btnDelete') {
                let flag = confirm('确认要删除吗？')
                if(!flag) return
                let result = await axios.get('/job/delete', {
                    params: {
                        jobId:jobid
                    }
                })
                if (result.code == 0) {
                    localStorage.removeItem('job')
                    $target.parent().parent().remove()
                    alert('删除成功') 
                }
                return
            }
        })
    }

    return {
        init() {
            bindJobList()
            delegateHandle()
        }
    }
})()
 joblist.init()
